package com.lysj.bhp.financial.controller;

import com.lysj.bhp.financial.base.FinancialContent;
import com.lysj.bhp.financial.domain.UserEarningsRecord;
import com.lysj.bhp.user.domain.User;
import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author zk
 * @description
 * @create 2018-07-20 18:55
 **/
@RestController
@RequestMapping("admin/user_earnings_record")
public class UserEarningsRecordController extends FinancialContent {


    @GetMapping("/show_earnings_record")
    public Resp list(Long userFinancialRecordId) {
        return new Resp(userEarningsRecordRepository.findByUserFinancialRecordId(userFinancialRecordId));
    }


    @GetMapping("list")
    public Resp list() {
        List<UserEarningsRecord> financialList = userEarningsRecordRepository.findAll();
        return new Resp(financialList);
    }

    @PostMapping("/add")
    public Resp add(UserEarningsRecord model){
        if(ParamUtil.isBlack(model.getUserId())){
            return  new Resp("请选择用户");
        }
        User user = userRepository.findOne(model.getUserId());
        if(user == null){
            return new Resp("用户不存在");
        }
        model.setEmail(user.getEmail());
        if(ParamUtil.isBlack(model.getFinancialName())){
            return  new Resp("请输入理财包名称");
        }
        if(ParamUtil.isBlack(model.getInvestmentPortion())){
            return  new Resp("请输入投资份额");
        }
        if(ParamUtil.isBlack(model.getExchangeRate())){
            return  new Resp("请输入当日汇率");
        }
        if(ParamUtil.isBlack(model.getDivided())){
            return  new Resp("请输入收益数量");
        }
        model.setCreateDate(new Date());
        userEarningsRecordRepository.save(model);
        return new Resp("添加成功");
    }
}
